package redis.gambing;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;

import java.util.Map;

import static redis.gambing.GambleFinalString.GAMBLER_ID;
import static redis.gambing.GambleFinalString.HASH_GAMBLER_1_MONEY;

public class GamblerMoneyInfoFilter extends BaseRichBolt {
    private OutputCollector collector;

    @Override
    public void prepare(Map<String, Object> topoConf, TopologyContext context, OutputCollector collector) {
        this.collector = collector;
    }

    @Override
    public void execute(Tuple input) {
        Long money = input.getLongByField(HASH_GAMBLER_1_MONEY);
        if (money != 0L) {
            collector.emit(new Values(input.getStringByField(GAMBLER_ID), input.getLongByField(HASH_GAMBLER_1_MONEY)));
        }
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields(GAMBLER_ID, HASH_GAMBLER_1_MONEY));
    }
}
